// 创建错误的插件
var createError = require("http-errors");
var express = require("express");
var path = require("path");
// 解析cookie
var cookieParser = require("cookie-parser");
// 日志插件
var logger = require("morgan");

// 路由
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var foodRouter = require("./routes/food");
var collectRouter = require("./routes/collect");
var foodRecordRouter = require("./routes/foodRecord");
var sportsRouter = require("./routes/sports");
var sportsRecordRouter = require("./routes/sportsRecord");
var utilsRouter = require("./routes/utils");
var collegeRouter = require("./routes/college");
var postRouter = require("./routes/post");
var commentRouter = require("./routes/comment");
var messageRouter = require("./routes/message");
var scoreRouter = require("./routes/score");
var fansRouter = require("./routes/fans");
var adminRouter = require("./server/routes/utils");
var goodsAdminRouter = require("./server/routes/goods");
var goodsRouter = require("./routes/goods");
var goodsConversionRouter = require("./routes/goodsConversion");
var topicCategoryAdminRouter = require("./server/routes/topicCategory");
var topicAdminRouter = require("./server/routes/topic");
var topicRouter = require("./routes/topic");
var activityAdminRouter = require("./server/routes/activity");
var activityRouter = require("./routes/activity");

var app = express();

// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

// 挂载路由
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/food", foodRouter);
app.use("/collect", collectRouter);
app.use("/foodrecord", foodRecordRouter);
app.use("/sports", sportsRouter);
app.use("/sportsrecord", sportsRecordRouter);
app.use("/util", utilsRouter);
app.use("/college", collegeRouter);
app.use("/post", postRouter);
app.use("/comment", commentRouter);
app.use("/message", messageRouter);
app.use("/score", scoreRouter);
app.use("/fans", fansRouter);
app.use("/admin", adminRouter);
app.use("/goodsAdmin", goodsAdminRouter);
app.use("/goods", goodsRouter);
app.use("/goodsConversion", goodsConversionRouter);
app.use("/topicCategoryAdmin", topicCategoryAdminRouter);
app.use("/topicAdmin", topicAdminRouter);
app.use("/topic", topicRouter);
app.use("/activityAdmin", activityAdminRouter);
app.use("/activity", activityRouter);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});

module.exports = app;
